package com.ruoyi.course.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.course.domain.SelectCourse;
import com.ruoyi.course.mapper.CourseSelectionMapper;
import com.ruoyi.course.mapper.SelectCourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CourseMapper;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;

/**
 * 课程Service业务层处理
 * 
 * @author Weieny
 * @date 2025-02-23
 */
@Service
public class CourseServiceImpl implements ICourseService 
{
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseSelectionMapper courseSelectionMapper;
    @Autowired
    private SelectCourseMapper selectCourseMapper;

    /**
     * 查询课程
     * 
     * @param courseId 课程主键
     * @return 课程
     */
    @Override
    public Course selectCourseByCourseId(Long courseId)
    {
        return courseMapper.selectCourseByCourseId(courseId);
    }

    /**
     * 查询课程列表
     * 
     * @param course 课程
     * @return 课程
     */
    @Override
    @DataScope(userAlias = "c") // 创建的课程只能自己查看
    public List<Course> selectCourseList(Course course)
    {
        return courseMapper.selectCourseList(course);
    }

    /**
     * 新增课程
     * 
     * @param course 课程
     * @return 结果
     */
    @Override
    public int insertCourse(Course course)
    {
        course.setCreateTime(DateUtils.getNowDate());
        return courseMapper.insertCourse(course);
    }

    /**
     * 修改课程
     * 
     * @param course 课程
     * @return 结果
     */
    @Override
    public int updateCourse(Course course)
    {
        course.setUpdateTime(DateUtils.getNowDate());
        return courseMapper.updateCourse(course);
    }

    /**
     * 批量删除课程
     * 
     * @param courseIds 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteCourseByCourseIds(Long[] courseIds)
    {
        return courseMapper.deleteCourseByCourseIds(courseIds);
    }

    /**
     * 删除课程信息
     * 
     * @param courseId 课程主键
     * @return 结果
     */
    @Override
    public int deleteCourseByCourseId(Long courseId)
    {
        return courseMapper.deleteCourseByCourseId(courseId);
    }

    /**
     * 修改课程状态
     * @param course 课程
     * @return
     */
    @Override
    public int updateCourseStatus(Course course) {
        return courseMapper.updateCourse(course);
    }

    /**
     * 查询我的课程表
     * @param userId 用户ID
     * @return 课程表集合
     */
    @Override
    public List<SelectCourse> getCourseTableList(Long userId) {
        // 首先根据userId从my_selected_course表中查出选课的courseIds列表
        List<Long> courseIds = courseSelectionMapper.getCourseIdsByUserId(userId);
        List<SelectCourse> courseList = new ArrayList<>();

        // 遍历courseIds，根据每个courseId从selectCourseMapper中查出SelectCourse对象
        for (Long courseId : courseIds) {
            SelectCourse course = selectCourseMapper.selectSelectCourseByCourseId(courseId);
            if (course != null) {
                courseList.add(course);
            }
        }

        return courseList;
    }
}
